# -*- coding:utf-8 -*-
import hashlib

"""整理收集字符串转换相关的代码

@convert_str_into_hex: Unicode字符串转换为十六进制字符串

@convert_hex_into_str: 十六进制字符串转换为Unicode字符串

@add_something_into_str: 字符串中每隔两个字符添加一个字符

@str2md5: 元字符串进行MD5编码
"""


def convert_str_into_hex(ascii_str: str) -> str:
    """把ASCII字符串转换为十六进制字符串"""
    ascii_str_list = list(ascii_str)
    hex_str_list = []
    for c in ascii_str_list:
        hex_str_list.append(hex(ord(c)))
    return "".join([c[2:] for c in hex_str_list])


def convert_hex_into_str(hex_str: str) -> str:
    """把十六进制字符串转换为ASCII字符串"""
    hex_str_list = []
    ascii_str_list = []
    # 制作一个十六进制字符串匹配十进制的map
    hex_map = {'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15}
    for i in range(10):
        hex_map[str(i)] = i

    for i in range(0, len(hex_str), 2):
        hex_str_list.append(hex_str[i:i + 2])
    for hex_chr in hex_str_list:
        ascii_num = hex_map.get(hex_chr[0]) * 16 + hex_map.get(hex_chr[1])
        ascii_str_list.append(chr(ascii_num))
    return "".join(ascii_str_list)


def add_something_into_str(s: str, adder: str) -> str:
    """把一个字符串每隔两个字符添加一个字符"""
    res = []
    for i in range(0, len(s), 2):
        res.append(f"{adder}" + s[i: i+2])
    return "".join(res)


def str2md5(string: str):
    """把元字符串进行MD5编码"""
    md5 = hashlib.md5()
    md5.update(string.encode("utf-8"))
    print(md5.hexdigest())


if __name__ == '__main__':
    pass
